package com.dataart.training.crimequalification.beans;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

@ManagedBean
@Component
@Scope("request")
public class LocationBean {

    // -----------------------------------------------------------------

    private static final Logger logger = LoggerFactory
            .getLogger(LocationBean.class);

    //-------------------------------------------------------------------------

    private static final String HOMEPAGE_VIEW_IDENTIFIER = "/views/index.xhtml";
    private static final String TEST_SMS_SENDER_VIEW_IDENTIFIER = "/views/testsmssender.xhtml";
    private static final String MANAGE_EVENTS_VIEW_IDENTIFIER = "/views/manageevents.xhtml";

    public String currentViewId;

    //-------------------------------------------------------------------------

    private boolean isCurrentViewIdEqualTo(String viewId) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        String currentViewId = facesContext.getViewRoot().getViewId();
        return viewId.equals(currentViewId);
    }

    public boolean isCurrentLocationHomepage() {
        return isCurrentViewIdEqualTo(HOMEPAGE_VIEW_IDENTIFIER);
    }

    public boolean isCurrentLocationTestSMSSender() {
        return isCurrentViewIdEqualTo(TEST_SMS_SENDER_VIEW_IDENTIFIER);
    }

    public boolean isCurrentLocationManageEvents() {
        logger.debug("Current location: " + FacesContext.getCurrentInstance().getViewRoot().getViewId());
        return isCurrentViewIdEqualTo(MANAGE_EVENTS_VIEW_IDENTIFIER);
    }

    //-------------------------------------------------------------------------

    public String getCurrentViewId() {
        return currentViewId;
    }

    public void setCurrentViewId(String currentViewId) {
        this.currentViewId = currentViewId;
    }

}
